package com.netease.nie.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.netease.nie.base.controllers.BaseController;
import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.base.protocol.SystemResponseCode;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.constants.PositionOfCreditConstants;
import com.netease.nie.dto.CreditInHomePageDto;
import com.netease.nie.dto.CreditInJFMallDto;
import com.netease.nie.dto.CreditInUserInfoDto;
import com.netease.nie.dto.UserInfoDto;
import com.netease.nie.service.CreditService;
import com.netease.nie.service.UserInfoService;

@RestController
@RequestMapping("/api/user")
public class UserInfoController extends BaseController {

	@Autowired
	private CreditService creditService;

	@Autowired
	private UserInfoService userInfoService;
	
	/**
	 * 用户信息、个人积分查询接口
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/base", method = RequestMethod.GET, produces = "application/json")
	public String list(HttpServletRequest request, Integer position) {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			return jsonFail(BusinessConstants.USER_NOT_LOGIN.getCode(),
					"用户未登录");
		if (userId <= 0)
			throw new BusinessException(
					BusinessConstants.INVALID_USER_ID.getCode(),
					"用户id不合法");
		
		if (position == PositionOfCreditConstants.HOME_PAGE.getCode()) {
			CreditInHomePageDto creditInHomePageDto = creditService
					.queryCreditInHomePageDtoByUserId(userId);
			return jsonSuccess(creditInHomePageDto);
		} else if (position == PositionOfCreditConstants.JF_MALL.getCode()) {
			CreditInJFMallDto creditInJFMallDto = creditService
					.queryCreditInJFMallDtoByUserId(userId);
			return jsonSuccess(creditInJFMallDto);
		} else if (position == PositionOfCreditConstants.USER_HOME_PAGE
				.getCode()) {
			CreditInUserInfoDto creditInUserInfoDto = creditService
					.queryCreditInUserInfoDtoByUserId(userId);
			return jsonSuccess(creditInUserInfoDto);
		}
		throw new BusinessException(
				SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(),
				SystemResponseCode.PARAM_VALUE_ILLEGAL.getMsg());
	}
	
	/**
	 * 个人资料编辑接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/edit_info", method = RequestMethod.GET, produces = "application/json")
	public String editInfo(HttpServletRequest request) {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			return jsonFail(BusinessConstants.USER_NOT_LOGIN.getCode(),
					"用户未登录");
		
		if (userId <= 0)
			throw new BusinessException(
					BusinessConstants.INVALID_USER_ID.getCode(),
					"用户id不合法");
		
		UserInfoDto userInfoDto = userInfoService.edit(userId);
		return jsonSuccess(userInfoDto);
	}
	
	/**
	 * 个人资料填写接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/save_info", method = RequestMethod.POST, produces = "application/json")
	public String saveInfo(HttpServletRequest request, @RequestBody UserInfoDto userInfoDto) {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			return jsonFail(BusinessConstants.USER_NOT_LOGIN.getCode(),
					"用户未登录");
		
		userInfoDto.setUserId(userId);
		int result = userInfoService.save(userInfoDto);
		return jsonSuccess(result);
	}
	
}
